package cn.snoworld.service.impl;

import cn.snoworld.entity.Article;
import cn.snoworld.entity.Comment;
import cn.snoworld.entity.ArticleContent;
import cn.snoworld.mapper.CommentMapper;
import cn.snoworld.mapper.ArticleContentMapper;
import cn.snoworld.mapper.ArticleMapper;
import cn.snoworld.service.ArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    @Autowired
    private CommentMapper articleCommentMapper;

    @Autowired
    private ArticleContentMapper articleContentMapper;

    @Override
    @Transactional
    public boolean save(Article entity) {
        try {
            // 添加记录
            baseMapper.insert(entity);

            // 添加评论信息
            Comment articleComment = new Comment(
                    null,
                    entity.getId(),
                    10000,
                    entity.getContent(),
                    null
            );
            articleCommentMapper.insert(articleComment);

            // 添加内容
            ArticleContent articleContent = new ArticleContent(
                    null,
                    entity.getId(),
                    entity.getContent()
            );
            articleContentMapper.insert(articleContent);

            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
